package com.totoro.controller.request;

import com.fasterxml.jackson.databind.JsonNode;
import com.totoro.pojo.MarketOrder;
import com.totoro.pojo.MarketSrOrder;
import com.totoro.pojo.PrimaryReturn;
import com.totoro.pojo.User;
import com.totoro.service.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author TOTORO
 * @date 2021/8/9 23:40
 * 退货款跳转控制器
 */
@Controller
@RequestMapping("/dispute")
public class ReturnRequestController {

    static Logger logger = Logger.getLogger(ReturnRequestController.class);
    @Autowired
    AddressService addressService;
    @Autowired
    UserService userService;
    @Autowired
    OrderService orderService;
    @Autowired
    EvaluationService evaluationService;
    @Autowired
    SellerService sellerService;
    @Autowired
    ReturnService returnService;

    /** 以下为买家方售后辩论 ----------------*/
    /**
     * 跳转至选择售后页面
     * @return
     */
    @RequestMapping("/applyRouter")
    public String goApplyRouter(@RequestParam("bizOrderId") String orderId, @RequestParam("detailId") int msrId, Model model){
        // 获得当前售后的总订单，详情订单
        MarketOrder marketOrder = orderService.findAllOrder(orderId, 2, 0).get(0);
        // 根据订单详情id获得指定的订单详情实体
        MarketSrOrder marketSrOrder = null;
        for (MarketSrOrder srOrder : marketOrder.getMarketSrOrderList()) {
            if(srOrder.getId() == msrId){
                marketSrOrder = srOrder;
                break;
            }
        }
        model.addAttribute(marketSrOrder);
        return "include/user/select_apply_after";
    }

    /**
     * 跳转至申请售后页面
     * @param orderId
     * @param msrId
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/requestApplyAfter")
    public String requestApplyAfter(@RequestParam("bizOrderId") String orderId, @RequestParam("detailId") int msrId, @RequestParam("returnType") int returnType,
                                    /** 该参数用于进入修改模式 -> */@RequestParam(value = "bizReturnOdd", required = false, defaultValue = "0") String returnOdd,
                                    Model model, HttpSession session){
        // 检测是否登录
//        User loginUser = (User) session.getAttribute("user");
//        if(null == loginUser){
//            return "redirect:/login.htm";
//        }
        // 获得当前售后的总订单，详情订单
        MarketOrder marketOrder = orderService.findAllOrder(orderId, 2, 0).get(0);
        // 根据订单详情id获得指定的订单详情实体
        MarketSrOrder marketSrOrder = null;
        for (MarketSrOrder srOrder : marketOrder.getMarketSrOrderList()) {
            if(srOrder.getId() == msrId){
                marketSrOrder = srOrder;
                break;
            }
        }

        // 获得这次订单的卖家给用户看
        User sellerUser = sellerService.findUserBySlid(marketOrder.getSlid());

        // 给数据前端渲染
        model.addAttribute("returnType", returnType);
        model.addAttribute("marketOrder", marketOrder);
        model.addAttribute("marketSrOrder", marketSrOrder);
        model.addAttribute("sellerUser", sellerUser);
        // 如果买家进入了修改模式
        boolean isUpdate = false;
        if(!returnOdd.equals("0")){
            isUpdate = true;
            // 根据退货款主要单号获得实体
            PrimaryReturn primaryReturn = returnService.findPrimaryReturnAllDetailByReturnOdd(returnOdd);
            model.addAttribute("primaryReturn", primaryReturn);
        }
        model.addAttribute("updateStatus", isUpdate);
        return "include/user/request_apply_after";
    }

    /**
     * 用户售后指定详情页
     * @param marketOdd
     * @param msroId
     * @param returnOdd
     * @param model
     * @return
     */
    @RequestMapping("/detail")
    public String goReturnDetail(@RequestParam("bizOrderId") String marketOdd, @RequestParam("detailId") int msroId,
                                 @RequestParam("bizReturnId") String returnOdd, Model model){
        // 根据参数上的单号和Id拿到所有的实体
        // 获得当前售后的总订单，详情订单
        MarketOrder marketOrder = orderService.findAllOrder(marketOdd, 2, 0).get(0);
        // 根据订单详情id获得指定的订单详情实体
        MarketSrOrder marketSrOrder = null;
        for (MarketSrOrder srOrder : marketOrder.getMarketSrOrderList()) {
            if(srOrder.getId() == msroId){
                marketSrOrder = srOrder;
                break;
            }
        }

        PrimaryReturn primaryReturn = returnService.findPrimaryReturnAllDetailByReturnOdd(returnOdd);
        // 获得这次订单的卖家给用户看
        User sellerUser = sellerService.findUserBySlid(marketOrder.getSlid());
        // 获得这次订单的买家给卖家看
        User buyerUser =sellerService.findUserByUid(marketOrder.getUid());
        // 存
        model.addAttribute("marketOrder", marketOrder);
        model.addAttribute("marketSrOrder", marketSrOrder);
        model.addAttribute("primaryReturn", primaryReturn);
        model.addAttribute("sellerUser", sellerUser);
        model.addAttribute("buyerUser", buyerUser);
        return "include/user/return_detail";
    }

    /**
     * 等待卖家处理售后
     * @return
     */
    @RequestMapping("/awaitAfter")
    public String goApplySuccess(){
        return "include/user/after_await_seller_dispose";
    }

    /**
     * 卖家已同意退货款
     * @return
     */
    @RequestMapping("/sellerAgreeReturn")
    public String goSellerAgreeReturn(){
        return "include/user/seller_agree_return";
    }

    /**
     * 成功全部处理完退货款页面
     * @return
     */
    @RequestMapping("/allReturnSuccess")
    public String goAllReturnSuccess(){
        return "include/user/all_return_success";
    }


//    用户退款管理

    /**
     * 跳转用户售后列表
     * @return
     */
    @RequestMapping("/buyerDisputeList")
    public String buyerDisputeList(){
        return "include/user/buyer_dispute_list";
    }
}
